<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class program extends CI_Controller {

	function __construct(){
            parent::__construct();
            $this->load->library(array('session','form_validation'));
            $this->load->helper(array('form', 'url'));
            $this->admin_id = $this->session->userdata('admin_id');
            $this->admin_username = $this->session->userdata('admin_username');
            $this->load->model('program_model');
            $this->load->model('admin_model');
        }
        
        public function authorize()
        {
            if($this->admin_id && $this->admin_model->get_admin($this->admin_id)->num_rows() > 0)return true;
            else
            {
                redirect('admin/login');
                return false;
            }
        }
        
	public function index()
	{
            $result = $this->authorize();
            if($result == false)return;
            
            $result = $this->program_model->get_program()->result_array();
            foreach($result as &$row)
            {
                $query = $this->program_model->get_tahun_by_id($row['TAHUN_ID'])->row();
                $row['TAHUN_NAMA'] = $query->TAHUN_NAMA;
            }
            
            $data['program_list'] = $result;
            $this->load->view('template/header');
            $this->load->view('program/list', $data);
            $this->load->view('admin/sidebar');
            $this->load->view('template/footer');
            
	}
        public function delete($program_id)
        {
            $result = $this->authorize();
            if($result == false)return;
            
            $this->program_model->delete_program($program_id);
            $this->index();
        }
        public function do_update()
        {
            $result = $this->authorize();
            if($result == false)return;

            $program_name = $this->input->post('program_name');
            $tahun = $this->input->post('tahun');
            $program_id = $this->input->post('program_id');
            
            if($this->program_model->get_prog_from_id($program_id)->num_rows() <= 0)
            {
                $this->index();
                return;
            }
            $data['tahun_list']=$this->program_model->get_list_tahun()->result_array();
            $data['submit_data']=array(
                'program_id' => $program_id,
                'program_name' => $this->input->post('program_name'),
                'tahun' => $this->input->post('tahun'),
            );
            
            $this->form_validation->set_message('required', "%s wajib di isi");
            $this->form_validation->set_message('min_length', "%s harus lebih dari 6 karakter");
            $this->form_validation->set_message('max_length', "%s tidak boleh lebih dari 12 karakter");
            $this->form_validation->set_message('valid_email', "%s tidak valid");
            $this->form_validation->set_message('numeric', "%s tidak valid, isikan nomor yang benar");
            
            $this->form_validation->set_rules('program_name', 'Nama Program', 'trim|required');
            $this->form_validation->set_rules('tahun', 'Tahun Program', 'trim|required');
            
            if ($this->form_validation->run() == FALSE)
            {
                $this->update($program_id);
            }
            else
            {
                $this->program_model->update_program($program_id,$tahun,$program_name);
                $this->index();
            }          
        }
        
        public function update($program_id)
        {
            $result = $this->authorize();
            if($result == false)return;
            $result = $this->program_model->get_prog_from_id($program_id);
            if($result->num_rows() <= 0)
            {
                $this->index();
                return;
            }
            else
            {
                $row = $result->row();
                $data['tahun_list']=$this->program_model->get_list_tahun()->result_array();
                $data['submit_data']=array(
                    'program_id' => $program_id,
                    'program_name' => $row->PROGRAM_NAMA,
                    'tahun_id' => $row->TAHUN_ID
                );   
                $this->load->view('template/header', $data);
                $this->load->view('program/update', $data);
                $this->load->view('admin/sidebar');
                $this->load->view('template/footer', $data);
                return;
            }
        }
        
        public function get_program_by_tahun()
        {
            $tahun_id = $this->input->post('tahun_box');
            $q = $this->program_model->get_program_by_tahun($tahun_id);
            echo "<option value=\"0\" >Pilih Program</option>";
            foreach ($q->result() as $r){
                echo "<option value=\"$r->PROGRAM_ID\" >$r->PROGRAM_NAMA</option>";
            }
        }
        
        public function insert_tahun()
        {
            $tahun = $this->input->post('tahun');
            $data['submit_data']=array(
                'tahun' => $this->input->post('tahun')
            );
            $this->form_validation->set_message('required', "%s wajib di isi");
            $this->form_validation->set_message('min_length', "%s harus lebih dari 6 karakter");
            $this->form_validation->set_message('max_length', "%s tidak boleh lebih dari 12 karakter");
            $this->form_validation->set_message('valid_email', "%s tidak valid");
            $this->form_validation->set_message('numeric', "%s tidak valid, isikan nomor yang benar");
            
            $this->form_validation->set_rules('tahun', 'Tahun Program', 'trim|numeric|required');
            if ($this->form_validation->run() == FALSE)
            {
                $this->load->view('template/header', $data);
                $this->load->view('program/insert_tahun', $data);
                $this->load->view('admin/sidebar');
                $this->load->view('template/footer', $data);
            }
            else
            {
                $this->program_model->insert_program_tahun($tahun);
                $this->index();
            }
        }
        
        public function insert()
        {
            $program_name = $this->input->post('program_name');
            $tahun = $this->input->post('tahun');
            
            $data['tahun_list']=$this->program_model->get_list_tahun()->result_array();
            $data['submit_data']=array(
                'program_name' => $this->input->post('program_name'),
                'tahun' => $this->input->post('tahun'),
            );
            
            $this->form_validation->set_message('required', "%s wajib di isi");
            $this->form_validation->set_message('min_length', "%s harus lebih dari 6 karakter");
            $this->form_validation->set_message('max_length', "%s tidak boleh lebih dari 12 karakter");
            $this->form_validation->set_message('valid_email', "%s tidak valid");
            $this->form_validation->set_message('numeric', "%s tidak valid, isikan nomor yang benar");
            
            $this->form_validation->set_rules('program_name', 'Nama Program', 'trim|required');
            $this->form_validation->set_rules('tahun', 'Tahun Program', 'trim|required');
            
            if ($this->form_validation->run() == FALSE)
            {
                $this->load->view('template/header', $data);
                $this->load->view('program/insert', $data);
                $this->load->view('admin/sidebar');
                $this->load->view('template/footer', $data);
            }
            else
            {
                $this->program_model->insert_program($tahun,$program_name);
                $this->index();
            }
        }
}